$(function(){
	var dataColums = [ [ {
		field : 'itemId',
		hidden : true
	}, {
		field : 'createdAt',
		title : '创建时间',
		width : 90,
		align : 'center',
		formatter : function(v) {
			return utils.format(v, 'yyyy-MM-dd');
		}
	},{
		field : 'company',
		title : '单位',
		width : 200,
		align : 'center'
	},{
		field : 'receiver',
		title : '收货人',
		width : 150,
		align : 'center'
	},
	{ 
		field : 'mobile',
		title : '联系方式',
		width : 150,
		align : 'center'
	}, {
		field : 'address',
		title : '地址',
		width : 150,
		align : 'center'
	}] ];
	var gridToolbar = [{
		text:'新增用户',
		iconCls: 'icon-add',
		handler: function(){
			clearUserForm();
			$('#customerOption').dialog({
			    title: '新增用户',
			    width: 400,
			    height: 200,
			    closed: false,
			    cache: false,
			    modal: true,
			    buttons:[{
					text:'确定',
					iconCls:'icon-ok',
					handler:saveBillCustomer
				}]
			});
		}
	},'-',{
		text:'编辑用户',
		iconCls: 'icon-edit',
		handler: function(){
			clearUserForm();
			var cc = getSelectCusotmer();
			$('#customerOption').find('input').each(function(index,input){
				input.value = cc[input.name];
			});
			$('#customerOption').dialog({
			    title: '编辑用户',
			    width: 400,
			    height: 200,
			    closed: false,
			    cache: false,
			    modal: true,
			    buttons:[{
					text:'确定',
					iconCls:'icon-ok',
					handler:saveBillCustomer
				}]
			});
		}
	}/*,'-',{
		text:'开票',
		iconCls: 'icon-add',
		handler: function(){
			var type = 1;
			transaction(getSelectCusotmer().id,type);
		}
	},'-',{
		text:'收款',
		iconCls: 'icon-add',
		handler: function(){
			var type = 2;
			transaction(getSelectCusotmer().id,type);
		}
	}*/];
	
	$("#receiverData").datagrid({
		columns : dataColums,
		toolbar: gridToolbar,
		singleSelect:true
	});
	queryCus();
});
function getSelectCusotmer(){
	return $("#receiverData").datagrid('getSelected');
}
function saveBillCustomer(){
	var obj = {};
	$('#customerOption').find('input').each(function(index,input){
		obj[input.name]=input.value;
	});
	utils.postJson({url:'/saveBillCustomer',data:obj,success:function(response){
		$.messager.alert('状态提示',response.result.msg,'Warning');
		if(response.result.status == 'success'){
			$('#customerOption').dialog('close');
			queryCus();
		}
	}});
}
function clearUserForm(){
	$('#customerOption').find('input').val('');
	$('#transaction').find('input').val('');
}
function queryCus(){
	var reqData = {};
	$('#form').find('input').each(function(index,input){
		reqData[input.name]=input.value;
	});
	utils.postJson({url:'/queryCustomerList',data:reqData,success:function(response){
		$("#receiverData").datagrid('loadData',response.result);
	}});
}
function transaction(customerId,type){
	clearUserForm();
	$('#transaction').dialog({
	    title: type==1?'开票':'收款',
	    width: 350,
	    height: 130,
	    closed: false,
	    cache: false,
	    modal: true,
	    buttons:[{
			text:'确定',
			iconCls:'icon-ok',
			handler:function(){
				var obj = {type:type,customerId:customerId};
				$('#transaction').find('input').each(function(index,input){
					obj[input.name]=input.value;
				});
				utils.postJson({url:'/customerTransaction',data:obj,success:function(response){
					$.messager.alert('状态提示','操作成功','Warning');
					$('#transaction').dialog('close');
				}});
			}
		}]
	});
}